package tq.com.tqteacher.mine.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.lang.ref.WeakReference;

import tq.com.tqcom.vh.ComSpacerViewHolder;
import tq.com.tqcom.vh.ComViewHolder;
import tq.com.tqcom.vh.CommonViewHolder;
import tq.com.tqteacher.R;

public class TQClassInfoAdapter extends RecyclerView.Adapter {
    private DataItem[] mDataItems = new DataItem[]{
            DataItem.spacer(),
            DataItem.instance("班级名称",  false, 0),
            DataItem.instance("所在城市",  true, 1),
            DataItem.spacer(),
            DataItem.instance("所属年级",  false, 2),
            DataItem.instance("班级编号",  true, 3)
    };
    private Context mContext;
    private String[] values;

    public TQClassInfoAdapter(Context pContext){mContext = pContext;}


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        if(pI == 1){
            return CommonViewHolder.sCommonViewHolder(mContext, R.layout.vh_classinfo_1_layout, pViewGroup)
                    .setHolderBinder(new CommonViewHolder.IViewHolder() {
                        @Override
                        public void bind(View pView, Object data) {
                            try{
                                ImageView lView = pView.findViewById(R.id.banpai);
                                RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.banpai1)
                                        .fallback(R.drawable.banpai1);
                                Glide.with(mContext).load(data).apply(lOptions).into(lView);
                            }catch (Exception e){}
                        }
                    });
        }
        else if(pI == 2){
            return ComSpacerViewHolder.instance(15, pViewGroup, mContext);
        }
        View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_edit_layout, pViewGroup, false);
        return new EditItem(lView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        if(pViewHolder instanceof ComSpacerViewHolder){
            ((ComSpacerViewHolder)pViewHolder).bindHeight(15);
        }else if(pViewHolder instanceof EditItem){
            int index = pI - 1;
            DataItem lItem = mDataItems[index];
            String v = null;
            if(values != null){
                v = values[lItem.key + 1];
            }

            ((EditItem)pViewHolder).bind(lItem, v);
        }

        if(values != null && pViewHolder instanceof CommonViewHolder){
            ((CommonViewHolder)pViewHolder).bindData(values[0]);
        }
    }

    @Override
    public int getItemCount() {
        return 1 + mDataItems.length;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0) return 1;

        int index = position - 1;
        DataItem lItem = mDataItems[index];
        if(lItem.isSpacer){
            return 2;
        }
        return 0;
    }

    public void bindInfo(String[] info){
        if(info == null || info.length < 5) return;

//        for (int i = 0; i < info.length; i++){
//            if(mDataItems[i].mEditItemWeakReference != null
//                    && mDataItems[i].mEditItemWeakReference.get() != null){
//                mDataItems[i].mEditItemWeakReference.get().mEditText.setText(info[i]);
//            }
//        }
        values = info;
        notifyDataSetChanged();
    }


    static class DataItem{
        private String title;
        private boolean hideLine;
        private int key;
        private boolean isSpacer;
//        private WeakReference<EditItem> mEditItemWeakReference;

        public static DataItem instance(String title, boolean pHideLine, int key){
            DataItem lItem = new DataItem();
            lItem.title = title;
            lItem.hideLine = pHideLine;
            lItem.key = key;
            lItem.isSpacer = false;
            return lItem;
        }

        public static DataItem spacer(){
            DataItem lItem = new DataItem();
            lItem.isSpacer = true;
            return lItem;
        }
    }
    class EditItem extends RecyclerView.ViewHolder{
        private TextView mTextView;
        private EditText mEditText;
        private TextView mLine;

        public EditItem(View pView){
            super(pView);

            mTextView = pView.findViewById(R.id.title);
            mEditText = pView.findViewById(R.id.input);
            mLine = pView.findViewById(R.id.line);

            setIsRecyclable(false);
        }

        public void bind(DataItem pItem, String v){
            if(pItem != null){
//                pItem.mEditItemWeakReference = new WeakReference<>(this);

                mTextView.setText(pItem.title);
                mLine.setVisibility(pItem.hideLine ? View.INVISIBLE : View.VISIBLE);

                if(v != null){
                    mEditText.setText(v);
                }
            }
        }
    }

}
